package com.xiaotu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xiaotu.cache.BattleCache;
import com.xiaotu.handler.BattleHandler;
import com.xiaotu.mapper.BattlePlayerInfoMapper;
import com.xiaotu.service.BattlePlayerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * @author zhangxiao
 * @create 2022-04-01 15:25
 */
@Service
public class BattlePlayerInfoServiceImpl implements BattlePlayerInfoService {

    @Autowired
    private BattleCache battleCache;

    @Autowired
    private BattlePlayerInfoMapper battlePlayerInfoMapper;

    @Override
    public Object getBattlePlayerInfo(Integer roomId) {
        CopyOnWriteArrayList<Session> battleHandlers = battleCache.BATTLE_ROOM_MAP.get(roomId);
        if (CollectionUtil.isNotEmpty(battleHandlers)) {
            List<Integer> playerIdList = battleHandlers.stream().filter(w -> ObjectUtil.isNotNull(w)).map(w -> Integer.valueOf(w.getUserProperties().get("playerId").toString())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(playerIdList))
                return battlePlayerInfoMapper.getBattlePlayerInfo(playerIdList);
        }
        return null;
    }
}
